package day11;

import java.util.concurrent.locks.ReentrantLock;

//a.使用公平锁ReentrantLock实现3个线程交替打印1-100的数字，保证每个线程获取锁的机会均等
public class Home1 {
    ReentrantLock lock=new ReentrantLock(true);
    public void test(){
        String name=Thread.currentThread().getName();
        for(int i=1;i<=100;i++){
            lock.lock();
            System.out.println(name+":"+i);
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        Home1 h=new Home1();
        String name=Thread.currentThread().getName();
        for(int j=0;j<3;j++){
            new Thread(()->{
                h.test();
            },"线程"+j).start();
        }
    }
}
